//
//  ZZCInspectController.m
//  ZZC-Finance
//
//  Created by mac on 2017/6/10.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCInspectController.h"
#import "ZZCTableViewCell.h"
#import "ZZCInspect.h"

static NSString *inspectCell = @"inspectCell";
@interface ZZCInspectController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *inspectTableView;
@property (strong, nonatomic) NSMutableArray *inspectArray;
@property (strong, nonatomic) NSArray *inspectData;
@property (copy  , nonatomic) NSString *total;//总金额
@end

@implementation ZZCInspectController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查看";
    self.view.backgroundColor = [UIColor colorWithHex:0xeaeaea];
    [self.view addSubview:self.inspectTableView];
    [self requestInspectData];
    [self bottomBtn];

}
- (void)bottomBtn {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, ZZCScreenH-113, ZZCScreenW, 49)];
    btn.backgroundColor = [UIColor colorWithHex:0xEA6E25];
    [btn setTitle:@"完成" forState:0];
    [btn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = FONT(19);
    [self.view addSubview:btn];
}

//完成按钮监听
- (void)doneClick {
    ZZCLog(@"你点击了完成按钮");
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)requestInspectData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_MYREGULAR parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            self.inspectData = [ZZCInspect mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            NSString * str = responseObject[@"data"][@"total"];
           
            self.total = [NSString stringWithFormat:@"U币：%.2f U币", [str doubleValue]];
            [self.inspectTableView reloadData];
        }
    }];
}

#pragma mark  - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //填你后台返个数
    return self.inspectData.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZZCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inspectCell];
    cell.inspectModel = self.inspectData[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *ubiLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ZZCScreenW, 60)];
    ubiLabel.backgroundColor = [UIColor whiteColor];
    ubiLabel.font = FONT(25);
    ubiLabel.textAlignment = NSTextAlignmentCenter;
    ubiLabel.text = self.total;
    return ubiLabel;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
- (UITableView *)inspectTableView {
    if (_inspectTableView == nil) {
        _inspectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ZZCScreenW, ZZCScreenH-113) style:UITableViewStyleGrouped];
        [_inspectTableView registerNib:[UINib nibWithNibName:@"ZZCTableViewCell" bundle:nil] forCellReuseIdentifier:inspectCell];
        _inspectTableView.backgroundColor = [UIColor colorWithHex:0xeaeaea];
        _inspectTableView.delegate = self;
        _inspectTableView.dataSource = self;
        _inspectTableView.separatorStyle = NO;
        _inspectTableView.showsVerticalScrollIndicator = NO;
        _inspectTableView.rowHeight = 200;
    }
    return _inspectTableView;
}



- (NSMutableArray *)inspectArray {
    if (_inspectArray == nil) {
        _inspectArray = [NSMutableArray array];
    }
    return _inspectArray;
}

- (NSArray *)inspectData {
    if (_inspectData == nil) {
        _inspectData = [NSArray array];
    }
    return _inspectData;
}
@end
